const user = require("../models/userDetail")
const message = require("../models/message")
const posting = require("../models/posting")
const replyPosting = require("../models/replyPosting")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            user.find({
                flags: {
                    $all: fields.flags
                }
            }, (err, assistants) => {
                if (err) {
                    res.json({ 'state': false, 'msg': err });
                } else {
                    let assis_uuid_list = []
                    for (let j = 0; j < assistants.length; j++) {
                        assis_uuid_list.push(assistants[j].UUID)
                    }

                    let find_message = () => {
                        return new Promise(function (resolve, reject) {
                            message.find({
                                UUID: {
                                    $in: assis_uuid_list
                                },
                                type: "answer_notification"
                            }, (err, messages) => {
                                if (err) {
                                    res.json({ 'state': false, 'msg': err });
                                } else {
                                    resolve(messages)
                                }
                            })
                        })
                    }

                    let find_posting = () => {
                        return new Promise(function (resolve, reject) {
                            posting.find({}, (err, postings) => {
                                if (err) {
                                    res.json({ 'state': false, 'msg': err });
                                } else {
                                    // resolve({ messages, postings })
                                    resolve(postings)
                                }
                            })
                        })
                    }

                    let find_reply = () => {
                        return new Promise(function (resolve, reject) {
                            replyPosting.find({}, (err, replies) => {
                                if (err) {
                                    res.json({ 'state': false, 'msg': err });
                                } else {
                                    // console.log(replies)
                                    // resolve({ messages, postings, replies })
                                    resolve(replies)
                                }
                            })
                        })
                    }



                    let return_data = ( messages, postings, replies ) => {
                        let data = []
                        assistants.forEach((asst => {
                            data.push({
                                UUID: asst.UUID,
                                name: asst.realName,
                                job_number: 0,
                                job_done_number: 0,
                                job_percent: 0,
                                post_number: 0
                            })
                        }))

                        let data_temp = data.map(ele => {
                            return ele.UUID
                        })
                        // console.log(postings)

                        postings.forEach(ele => {
                            let target_index = data_temp.findIndex(temp_uuid => {
                                return temp_uuid == ele.authorUUID
                            })
                            if (target_index >= 0) {
                                data[target_index].post_number++
                            }
                        })

                        replies.forEach(ele => {
                            let target_index = data_temp.findIndex(temp_uuid => {
                                return temp_uuid == ele.authorUUID
                            })
                            if (target_index >= 0) {
                                data[target_index].post_number++
                            }
                        })

                        messages.forEach(msg => {
                            let data_temp = data.map(ele => {
                                return ele.UUID
                            })
                            let target_index = data_temp.findIndex(temp_uuid => {
                                return temp_uuid == msg.UUID
                            })
                            if(target_index>=0){
                                data[target_index].job_number++
                            }
                            if (msg.status != "unread") data[target_index].job_done_number++
                        })

                        data.forEach(assi => {
                            if (assi.job_number == 0) assi.job_percent = 0
                            else assi.job_percent = assi.job_done_number * 100 / assi.job_number
                        })

                        res.json({
                            state: true,
                            data
                        });
                    }

                    // find_message().then(find_posting).then(find_reply).then(return_data)
                    Promise.all([find_message(), find_posting(), find_reply()]).then(res => {
                        return_data(...res)
                    })

                }
            })
        }

    });

});

module.exports = router;